<template>
  <div>
    <!-- 店端库存冻结 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <FilterTables
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjInventoryFreeze"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)" :disabled="lockDisable != 2">冻 结</multiuse-button>
        <multiuse-button size="small" type="primary" @click="addHandle(2)" :disabled="lockDisable != 1">解 锁</multiuse-button>
      </template>
    </FilterTables>
    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="ownercloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="isLock" :title="'冻结'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <lockModel :list="selectList" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, postonLock, vehicleConfiguration, getWarehouseList } from '@/api/dealerSale/InventoryManagement/InventoryManagement';
import { dictionary } from '@/utils/dictMixin';
import lockModel from './lockModel';
import ownerSeacher from './ownerSeacher';
export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        isDealer: 0,
        freezingStatus: 10041001,
        xhrAbort: true
      },
      formData: {},
      isDetail: false,
      isLock: false,
      lockDisable: 0,
      selectList: [],
      issearch: false
    };
  },
  mixins: [dictionary],
  components: {
    lockModel,
    ownerSeacher
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getList();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '车辆状态',
          fieldName: 'vehicleStatus',
          placeholder: '请选择',
          itemList: [
            { text: '经销商在途', value: 60401002 },
            { text: '经销商库存', value: 60041003 }
          ]
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
          itemList: []
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productCode',
          placeholder: '请选择'
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '是否锁定',
          fieldName: 'isLock',
          placeholder: '请选择',
          itemList: this.createDictList('1004') || []
        },
        {
          type: 'RANGE_DATE',
          label: '店端入库日期',
          fieldName: 'beginPostingDate|endPostingDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '是否冻结',
          fieldName: 'isFreeze',
          placeholder: '请选择',
          itemList: this.createDictList('1004') || []
        },
        {
          type: 'SELECT',
          label: '退车标记',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: this.createDictList('1004') || []
        },

        {
          type: 'INPUT',
          label: '零售订单号',
          fieldName: 'orderNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'doNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '客户姓名',
          fieldName: 'customerName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008') || []
        },
        {
          type: 'INPUT',
          label: '冻结原因',
          fieldName: 'freezeReason',
          placeholder: '请输入'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '车辆状态',
          dataIndex: 'vehicleStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '经销商在途', value: 60401002 },
            { text: '经销商库存', value: 60041003 }
          ],
          render: p => {
            return <span>{this.createDictText(p.row.vehicleStatus, '6040')}</span>;
          }
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否冻结',
          dataIndex: 'isFreeze',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isFreeze, '1004')}</span>;
          }
        },
        {
          title: '冻结原因',
          dataIndex: 'freezeReason',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '冻结时间',
          dataIndex: 'freezeDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '实销配车',
          dataIndex: 'distributionStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6041'),
          render: p => {
            return <span>{this.createDictText(p.row.distributionStatus, '6041')}</span>;
          }
        },
        {
          title: '是否锁定',
          dataIndex: 'isLock',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isLock, '1004')}</span>;
          }
        },
        {
          title: '锁定备注',
          dataIndex: 'lockRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库名称',
          dataIndex: 'dlrWarehouseId',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: p => {
            return <span>{this.createDictText(p.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '6006')}</span>;
          }
        },
        {
          title: '订单细分',
          dataIndex: 'orderDetailTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否三方车辆',
          dataIndex: 'isTripartite',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isTripartite, '1004')}</span>;
          }
        },
        {
          title: '三方票号',
          dataIndex: 'biilNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '零售订单号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'packageCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionCode',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰色',
          dataIndex: 'trimCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰色',
          dataIndex: 'colorCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车龄(天)',
          dataIndex: 'productDateAge',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '库龄(天)',
          dataIndex: 'receiveDateAge',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '生产下线日期',
          dataIndex: 'productDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '销司出库日期',
          dataIndex: 'certificateDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发车日期',
          dataIndex: 'chngDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '店端入库日期',
          dataIndex: 'receiveDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '店端配车日期',
          dataIndex: 'distributionDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '店端出库日期',
          dataIndex: 'stockOutData',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '店端实销日期',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上牌日期',
          dataIndex: 'plateDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发动机号',
          dataIndex: 'engineNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '公告号',
          dataIndex: 'announceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '合格证号',
          dataIndex: 'certificateNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电机号',
          dataIndex: 'motorNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电池号',
          dataIndex: 'batteryNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退车标识',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '1004')}</span>;
          }
        }
      ];
    },
    async getList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        let isOpen = 0;
        let isClose = 0;
        row.map(x => {
          if (x.isFreeze == 10041001) {
            isOpen = isOpen + 1;
          } else {
            isClose = isClose + 1;
          }
        });
        if (isOpen > 0 && isClose == 0) {
          this.lockDisable = 1;
        } else if (isOpen == 0 && isClose > 0) {
          this.lockDisable = 2;
        } else {
          this.lockDisable = 0;
        }
        this.selectList = row;
      } else {
        this.lockDisable = 0;
        this.selectList = [];
      }
    },
    seacherFun() {
      this.issearch = true;
    },
    async addHandle(type) {
      if (type == 1) {
        this.isLock = true;
      } else if (type == 2) {
        await this.$confirm('是否解锁?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectList.map(x => {
              arr.push(`${x.vehicleId}`);
            });
            let res = await postonLock({ type: 2, vehicleId: arr, status: 10041002 });

            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '解锁成功',
                type: 'success'
              });
              this.lockDisable = 0;
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      }
    },
    changeHandle(params) {
      this.lockDisable = 0;
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.isDetail = true;
    },
    addcloseHandler(val) {
      this.isDetail = false;
      this.lockDisable = 0;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    closeHandler(val) {
      this.isLock = false;

      if (val) {
        this.lockDisable = 0;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    ownercloseHandler(val, data) {
      if (data) {
        let dealerCodes = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
      this.issearch = false;
    },
    resetChange(val) {
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
